package android.cs2340UDT;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

/*
 * Class handles creating an item and then adding it to the user
 */
public class CreateItemActivity extends Activity{

	Manager manage = Security.getManager();
	
	Item newItem = new Item("","","","");
	
	EditText inputDescription;
    EditText inputLocation;
    EditText inputDueDate;
    EditText inputComment;
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Displays the login screen
        setContentView(R.layout.taskscreen);
        
        // These retrieve the text input boxes
        final EditText inputDescription = (EditText) findViewById(R.id.name);
        final EditText inputLocation = (EditText) findViewById(R.id.location);
        final EditText inputDueDateMonth = (EditText) findViewById(R.id.month);
        final EditText inputDueDateDay = (EditText) findViewById(R.id.day);
        final EditText inputDueDateYear = (EditText) findViewById(R.id.year);
        final EditText inputComment = (EditText) findViewById(R.id.comment);
        Button createTaskButton = (Button) findViewById(R.id.createTaskButton);
        
        //This is when the user clicks the create a task button
        createTaskButton.setOnClickListener(new View.OnClickListener() {
        	 
            public void onClick(View arg0) {
            	
            	String name = inputDescription.getText().toString();
            	String location = inputLocation.getText().toString();
            	String month = inputDueDateMonth.getText().toString();
            	String day = inputDueDateDay.getText().toString();
            	String year = inputDueDateYear.getText().toString();
            	String comment = inputComment.getText().toString();
            	
            	int result = newItem.checkInfo(name, location, month, day, year, comment);
            		
            	if(result == 0) {
	            	//adds all of the input and adds it to the item
	            	newItem.setDescription(name);
	            	newItem.setLocation(location);
	            	//int temp = Integer.parseInt(((month.concat(day)).concat(year)));
	            	newItem.setDueDate(((month.concat(day)).concat(year)));
	            	newItem.setComment(comment);
	            	newItem.setCategory(ListViewActivity.getTempCategory());
	            	
	            	//needs to add new item to the users toDoList
	            	// ListViewActivity.getTempList().add(newItem);
	            	manage.addItemToCurrentList(newItem);
	            	
	            	//Creates a new location for our map
	            	ItemLocation i = new ItemLocation(location,name,comment);
	            	manage.addItemLocation(i);
	            	
	            	//Starting a new Intent
	        		Intent nextScreen = new Intent(getApplicationContext(), ListViewActivity.class);
	        		finish();
	        		//Switches over to MainScreenActivity
	        		startActivity(nextScreen);
            	}
            	else if(result == 1) {
            		// due date error popup
            	}
            	else {
            		// unknown error popup
            	}
            }
        });
        
        ImageButton backButton = (ImageButton) findViewById(R.id.back);
        
        //This is when the user clicks the back button
        backButton.setOnClickListener(new View.OnClickListener() {
       	 
            public void onClick(View arg0) {
            	//Starting a new Intent
        		Intent nextScreen = new Intent(getApplicationContext(), ListViewActivity.class);
        		finish();
        		//Switches over to MainScreenActivity
        		startActivity(nextScreen);
            }
        });
    }
}
